package app.server.project.main.service.db.entity.user;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "account")
@TableName("account")
@Comment(value = "账号信息表")
public class AccountEntity {

    @Id
    @Column(name = "user_id")
    @Comment("用户id")
    @TableId("user_id")
    private long userId;

    @Column(name = "open_id")
    @Comment("用户主键")
    @TableField("open_id")
    private String openId;


    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;


    @Column(name = "is_del")
    @TableField("is_del")
    @Comment("删除标志")
    private int isDel;

    @Column(name = "channel")
    @TableField("channel")
    @Comment("渠道标志")
    private int channel;


    @Column(name = "is_lock")
    @TableField("is_lock")
    @Comment("封号")
    private int isLock;


    @Column(name = "ad_channel")
    @Comment("广告渠道")
    @TableField("ad_channel")
    private String adChannel;

    @TableField("ip")
    @Comment("ip")
    @Column(name = "ip")
    private String ip;

    @TableField("country")
    @Comment("国家")
    @Column(name = "country")
    private String country;

    @TableField("lbs")
    @Comment("定位详情")
    @Column(name = "lbs", length = Integer.MAX_VALUE)
    private String lbs;
    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static AccountEntity of(String openId, long userId, int channel) {
        var ret = new AccountEntity();
        ret.setSynDb(true);
        ret.setUserId(userId);
        ret.setOpenId(openId);
        ret.setChannel(channel);
        ret.setCreateTime(LocalDateTime.now());
        ret.setIsDel(Def.FALSE);
        ret.setIsLock(Def.FALSE);
        return ret;
    }

    public static AccountEntity ofNull() {
        var ret = new AccountEntity();
        return ret;
    }

    public void setCountry(String country) {
        this.country = country;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getCountry, this);
    }

    public void setLbs(String lbs) {
        this.lbs = lbs;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getLbs, this);
    }

    public void setIp(String ip) {
        this.ip = ip;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getIp, this);
    }

    public void setAdChannel(String adChannel) {
        this.adChannel = adChannel;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getAdChannel, this);
    }

    @JsonIgnore
    public boolean getLock() {
        return isLock == Def.True;
    }

    public void setIsLock(int isLock) {
        this.isLock = isLock;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getIsLock, this);
    }

    public void setOpenId(String openId) {
        this.openId = openId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getOpenId, this);
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getCreateTime, this);
    }

    public void setIsDel(int isDel) {
        this.isDel = isDel;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getIsDel, this);
    }

    public void setChannel(int channel) {
        this.channel = channel;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(AccountEntity::getChannel, this);
    }
}
